﻿using System.ComponentModel;
using System.Linq;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows.Controls;

namespace OpenLightGroup.Common.Behaviors
{
    /// <summary>
    /// Toggles the IsEnabled of the Control based on the current user's roles.
    /// </summary>
    [Description("Toggles the IsEnabled of the Control based on the current user's roles.")]
    public class RoleBasedEnabledBehavior
         : BaseRoleBasedBehavior<Control> 
    {

        /// <summary>
        /// Updates the associated control.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        protected override void UpdateAssociatedObject(object sender, AuthenticationEventArgs e)
        {

            this.AssociatedObject.IsEnabled =
                (this.AllowedRoles.Where(r => IsInRole(r)).Count() > 0);

        }

    }
}
